#include "MCU.h"
#include "Task.h"
#include "Task_LCfg.h"
#include "Driver.h"
#include "General.h"
#include "Tps929120.h"
#include "Tps929120_PBCfg.h"
#include "Driver.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Variables
 ******************************************************************************/
uint8_t g_u8Left_status = 0;
uint8_t g_u8Right_status = 0;
uint8_t g_u8Turn_status = 0;
uint8_t g_u8Left_g_u8Board_lost_status = 0;
uint8_t g_u8Right_g_u8Board_lost_status = 0;
uint8_t g_u8Board_lost_status = 0;
uint8_t g_u8Diag_Databuff[16];
uint8_t g_u8Diag_Databuff_Right[16];

static Trunk_Diag_Status_t s_st_Trunk_DiagStatus;
static Fender_Diag_Status_t s_st_Fender_DiagStatus;

/* Input capture configuration for flexTimer_ic1 */
 ftm_input_param_t flexTimer_ic1_InputCaptureConfig2 =
{
     2U, /* Number of channels */
     18181U, /* Max count value */
     flexTimer_ic1_InputCaptureChannelConfig /* Channels configuration */
};

/*******************************************************************************
 * Static Functions
 ******************************************************************************/
static void MCU_SetPwmDuty_PTE8(uint32_t duty)
{
	uint8_t channel = pwm_pal1Configs.pwmChannels[0].channel;
	PWM_UpdateDuty(&pwm_pal1Instance, channel, duty);
}

/*Functions********************************************************************/
void Stop_On_Fender(void)
{
    MCU_SetPwmDuty_PTE8(5000);
}

/*Functions********************************************************************/
void Safety_LightOn(void)
{
    MCU_SetPwmDuty_PTE8(500);    // 500 / FTM_PWM_DUTY_MAX = 10%
}

/*Functions********************************************************************/
void Stop_Off_Fender(void)
{
    MCU_SetPwmDuty_PTE8(0);
}

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))

void PO_ALight_On(void)
{
	for(uint8_t i=0; i<6; i++)
	{
		g_16FenderSetPwmDuty[TPS_CHIP_ADD_1][i] = 0x398;	//22.5%
	}

	g_16FenderSetPwmDuty[TPS_CHIP_ADD_1][6] = 0xFFF;
	g_16FenderSetPwmDuty[TPS_CHIP_ADD_1][7] = 0xFFF;
}

void PO_ALight_Off(void)
{
	// g_16FenderSetChannel[TPS_CHIP_ADD_1] = 0;
	for(uint8_t i=0; i<12; i++)
	{
		g_16FenderSetPwmDuty[TPS_CHIP_ADD_1][i] = 0x0;
	}
}

void DI_ALight_On(void)
{
    uint8_t pwmduty_8byte[8] = {0};
	uint8_t pwmduty_4byte[4] = {0};
	
	pwmduty_8byte[0] = 0xFF;
	pwmduty_8byte[1] = 0xFF;
	pwmduty_8byte[2] = 0xFF;
	pwmduty_8byte[3] = 0xFF;
	pwmduty_8byte[4] = 0xFF;
	pwmduty_8byte[5] = 0xFF;
	pwmduty_8byte[6] = 0xFF;
	pwmduty_8byte[7] = 0xFF;
	pwmduty_4byte[0] = 0xFF;
	pwmduty_4byte[1] = 0xFF;
	
	Tps_WriteRegsBlocking(TPS_CHIP_ADD_2, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(TPS_CHIP_ADD_2, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

void DI_ALight_Off(void)
{
	uint8_t pwmduty_8byte[8] = {0};
	uint8_t pwmduty_4byte[4] = {0};
	
	pwmduty_8byte[0] = 0x00;
	pwmduty_8byte[1] = 0x00;
	pwmduty_8byte[2] = 0x00;
	pwmduty_8byte[3] = 0x00;
	pwmduty_8byte[4] = 0x00;
	pwmduty_8byte[5] = 0x00;
	pwmduty_8byte[6] = 0x00;
	pwmduty_8byte[7] = 0x00;
	pwmduty_4byte[0] = 0x00;
	pwmduty_4byte[1] = 0x00;
	
	Tps_WriteRegsBlocking(TPS_CHIP_ADD_2, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(TPS_CHIP_ADD_2, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

void DI_ALight_FlowOn(uint8_t led_position)
{
	uint8_t pwmduty_8byte[8] = {0, 0, 0, 0, 0, 0, 0, 0};
	uint8_t pwmduty_4byte[4] = {0, 0, 0, 0};

	switch (led_position)
	{
	case 5:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0x00;
		pwmduty_8byte[3] = 0x00;
		pwmduty_8byte[4] = 0x00;
		pwmduty_8byte[5] = 0x00;
		pwmduty_8byte[6] = 0x00;
		pwmduty_8byte[7] = 0x00;
		pwmduty_4byte[0] = 0x00;
		pwmduty_4byte[1] = 0x00;
		break;
	case 6:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0x00;
		pwmduty_8byte[5] = 0x00;
		pwmduty_8byte[6] = 0x00;
		pwmduty_8byte[7] = 0x00;
		pwmduty_4byte[0] = 0x00;
		pwmduty_4byte[1] = 0x00;
		break;
	case 7:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0x00;
		pwmduty_8byte[7] = 0x00;
		pwmduty_4byte[0] = 0x00;
		pwmduty_4byte[1] = 0x00;
		break;
	case 8:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0x00;
		pwmduty_4byte[1] = 0x00;
		break;
	case 9:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 10:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	
	default:
		break;
	}	

	Tps_WriteRegsBlocking(TPS_CHIP_ADD_2, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(TPS_CHIP_ADD_2, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

uint8_t Tail_ALight_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig14);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

uint8_t Turn_ALight_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig13);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

uint8_t Stop_ALight_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig9);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

#endif

#if CURRENT_PROJECT  == PROJECT_trunk

void PO_Left_On(void)
{

	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_0][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_1][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_2][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_3][i] = 0xFFF;

		g_16SetPwmDuty[TPS_CHIP_ADD_8][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_9][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_10][i] = 0xFFF;
	}

	// g_16SetPwmDuty[TPS_CHIP_ADD_3][0] = 0;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][1] = 0;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][2] = 0xFFF;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][3] = 0x333;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][4] = 0xF;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][5] = 0xFFF;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][6] = 0;
	// g_16SetPwmDuty[TPS_CHIP_ADD_3][7] = 0;
}

void PO_Left_Off(void)
{
	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_0][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_1][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_2][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_3][i] = 0;

		g_16SetPwmDuty[TPS_CHIP_ADD_8][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_9][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_10][i] = 0;
	}
}

void PO_Right_On(void)
{


	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_4][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_5][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_6][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_7][i] = 0xFFF;

		g_16SetPwmDuty[TPS_CHIP_ADD_11][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_12][i] = 0xFFF;
		g_16SetPwmDuty[TPS_CHIP_ADD_13][i] = 0xFFF;
	}
}

void PO_Right_Off(void)
{
	for(uint8_t i=0; i<12; i++)
	{
		g_16SetPwmDuty[TPS_CHIP_ADD_4][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_5][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_6][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_7][i] = 0;

		g_16SetPwmDuty[TPS_CHIP_ADD_11][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_12][i] = 0;
		g_16SetPwmDuty[TPS_CHIP_ADD_13][i] = 0;
	}
}

void DI_On(uint8_t direction)
{
	uint8_t pwmduty_8byte[8];
	uint8_t pwmduty_4byte[4];

	for(uint8_t i=0; i<8; i++)
	{
		pwmduty_8byte[i] = 0xFF;
	}
	for(uint8_t i=0; i<4; i++)
	{
		pwmduty_4byte[i] = 0xFF;
	}

	if(direction == 1)
	{
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}
	
	if(direction == 2)
	{
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}

}

void DI_Off(uint8_t direction)
{
	uint8_t dev_turn = 0;
	uint8_t pwmduty_8byte[8] = {0};
	uint8_t pwmduty_4byte[4] = {0};

	for(uint8_t i=0; i<8; i++)
	{
		pwmduty_8byte[i] = 0x0;
	}
	for(uint8_t i=0; i<4; i++)
	{
		pwmduty_4byte[i] = 0x0;
	}

	if(direction == 1)
	{

		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_14, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}
	
	if(direction == 2)
	{
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
		Tps_WriteRegsBlocking(TPS_CHIP_ADD_15, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
	}

}



uint8_t Turn_Left_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig11);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

uint8_t Turn_Right_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig9);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

uint8_t Tail_Left_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig14);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

uint8_t Tail_Right_ADC(void)
{
	uint16_t ADC_result = 0;

	ADC_DRV_ConfigChan(INST_ADCONV1, 0, &adConv1_ChnConfig10);

	ADC_DRV_WaitConvDone(INST_ADCONV1);

	ADC_DRV_GetChanResult(INST_ADCONV1,0,&ADC_result);

	if(ADC_result > 70) 
	{
		return ON;
	}
	else	
	{
		return OFF;
	}
}

void DI_FlowOn(uint8_t direction, uint8_t led_position)
{
	uint8_t dev_turn = 0;
	uint8_t pwmduty_8byte[8] = {0};
	uint8_t pwmduty_4byte[4] = {0};
	uint16_t flowon_enablebit[5] = {0x300, 0x3C0, 0x3F0, 0x3FC, 0xFFF};


	if(direction == 1)
	{
		dev_turn = TPS_CHIP_ADD_14;
	}

	if(direction == 2)
	{
		dev_turn = TPS_CHIP_ADD_15;
	}

	switch (led_position)
	{
	case 0:
		for(uint8_t i=0; i<8; i++) 
		{
			pwmduty_8byte[i] = 0;
		}
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 1:
		for(uint8_t i=0; i<6; i++) 
		{
			pwmduty_8byte[i] = 0;
		}
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 2:
		for(uint8_t i=0; i<4; i++) 
		{
			pwmduty_8byte[i] = 0;
		}
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 3:
		pwmduty_8byte[0] = 0x00;
		pwmduty_8byte[1] = 0x00;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 4:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	case 5:
		pwmduty_8byte[0] = 0xFF;
		pwmduty_8byte[1] = 0xFF;
		pwmduty_8byte[2] = 0xFF;
		pwmduty_8byte[3] = 0xFF;
		pwmduty_8byte[4] = 0xFF;
		pwmduty_8byte[5] = 0xFF;
		pwmduty_8byte[6] = 0xFF;
		pwmduty_8byte[7] = 0xFF;
		pwmduty_4byte[0] = 0xFF;
		pwmduty_4byte[1] = 0xFF;
		break;
	
	default:
		break;
	}	

	Tps_WriteRegsBlocking(dev_turn, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(dev_turn, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

#endif




void Process_Diag_Result(uint8_t position)
{
#if CURRENT_PROJECT  == PROJECT_trunk

	if(position == 1)
	{
		if(s_st_Trunk_DiagStatus.Open_Short_Fault_Left == 1)
		{
			g_u8Left_status++;
		}
		else
		{
			if(g_u8Left_status != 0)
			{
				g_u8Left_status--;
			}
		}
		if(g_u8Left_status >= 2)
		{
			Pin_PullDown(1);
			if(g_u8Left_status > 2)
			{
				g_u8Left_status = 2;
			}
		}

		if(s_st_Trunk_DiagStatus.Board_Loss_Fault_Left == 1)
		{
			g_u8Left_g_u8Board_lost_status++;
		}
		else
		{
			if(g_u8Left_g_u8Board_lost_status != 0)
			{
				g_u8Left_g_u8Board_lost_status--;
			}
		}
		if(g_u8Left_g_u8Board_lost_status >= 10)
		{
			Pin_PullDown(1);
			if(g_u8Left_g_u8Board_lost_status > 10)
			{
				g_u8Left_g_u8Board_lost_status = 10;
			}
		}
		
		if((g_u8Left_g_u8Board_lost_status == 0) && (g_u8Left_status == 0))
		{
			Pin_PullUp(1);
		}
	}

	if(position == 2)
	{
		if(s_st_Trunk_DiagStatus.Open_Short_Fault_Right == 1)
		{
			g_u8Right_status++;
		}
		else
		{
			if(g_u8Right_status != 0)
			{
				g_u8Right_status--;
			}
		}

		if(g_u8Right_status >= 2)
		{
			Pin_PullDown(2);
			if(g_u8Right_status > 2)
			{
				g_u8Right_status = 2;
			}
		}


		if(s_st_Trunk_DiagStatus.Board_Loss_Fault_Right == 1)
		{
			g_u8Right_g_u8Board_lost_status++;
		}
		else
		{
			if(g_u8Right_g_u8Board_lost_status != 0)
			{
				g_u8Right_g_u8Board_lost_status--;
			}
		}

		if(g_u8Right_g_u8Board_lost_status >= 10)
		{
			Pin_PullDown(2);
			if(g_u8Right_g_u8Board_lost_status > 10)
			{
				g_u8Right_g_u8Board_lost_status = 10;
			}
		}
		
		if((g_u8Right_g_u8Board_lost_status == 0) && (g_u8Right_status == 0))
		{
			Pin_PullUp(2);
		}
	}
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
	if(position == 1)
	{
		if(s_st_Fender_DiagStatus.Open_Short_Fault == 1)
		{
			g_u8Turn_status++;
		}
		else
		{
			if(g_u8Turn_status != 0)
			{
				g_u8Turn_status--;
			}
		}

		if(g_u8Turn_status >= 2)
		{
			Pin_PullDown(1);
			if(g_u8Turn_status > 2)
			{
				g_u8Turn_status = 2;
			}
		}

		if(s_st_Fender_DiagStatus.Board_Loss_Fault == 1)
		{
			g_u8Board_lost_status++;
		}
		else
		{
			if(g_u8Board_lost_status != 0)
			{
				g_u8Board_lost_status--;
			}
		}

		if(g_u8Board_lost_status >= 10)
		{
			Pin_PullDown(1);
			if(g_u8Board_lost_status > 10)
			{
				g_u8Board_lost_status = 10;
			}
		}
		
		if((g_u8Board_lost_status == 0) && (g_u8Turn_status == 0))
		{
			Pin_PullUp(1);
		}
	}
#endif
}

void Judge_Diag_Data(uint8_t position)
{
#if CURRENT_PROJECT  == PROJECT_trunk
	if(position == 1)
	{
		/*-------------Open/Short Fault----------------*/
		if(g_u8Diag_Databuff[5] == Tps_CrcCalc(&g_u8Diag_Databuff[4], 1))
		{
			if((g_u8Diag_Databuff[4] & 0x10) == 0x10)
			{
				//open-circuit fault/short-circuit fault
				if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_14) 
				{
					s_st_Trunk_DiagStatus.Open_Short_Fault_Left = 1;
				}
			}
			else
			{
				if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_14) 
				{
					s_st_Trunk_DiagStatus.Open_Short_Fault_Left = 0;
				}
			}
		}

		if(g_u8Diag_Databuff[7] == 0xFF)
		{
			//board lost fault
			if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_14) 
			{
				s_st_Trunk_DiagStatus.Board_Loss_Fault_Left = 1;
				g_u8Diag_Databuff[7] = 0x00;
			}
		}
		else
		{
			if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_14) 
			{
				s_st_Trunk_DiagStatus.Board_Loss_Fault_Left = 0;
			}
		}
	}

	if(position == 2)
	{
		if(g_u8Diag_Databuff_Right[5] == Tps_CrcCalc(&g_u8Diag_Databuff_Right[4], 1))
		{
			if((g_u8Diag_Databuff_Right[4] & 0x10) == 0x10)
			{
				if(g_u8Diag_Databuff_Right[1] == TPS_CHIP_ADD_15)
				{
					s_st_Trunk_DiagStatus.Open_Short_Fault_Right = 1;
				}
			}
			else
			{
				if(g_u8Diag_Databuff_Right[1] == TPS_CHIP_ADD_15)
				{
					s_st_Trunk_DiagStatus.Open_Short_Fault_Right = 0;
				}
			}
		}

		/*------------Board Loss Fault----------------*/
		if(g_u8Diag_Databuff_Right[7] == 0xFF)
		{
			if(g_u8Diag_Databuff_Right[1] == TPS_CHIP_ADD_15)
			{
				s_st_Trunk_DiagStatus.Board_Loss_Fault_Right = 1;
				g_u8Diag_Databuff_Right[7] = 0x00;
			}
		}
		else
		{
			if(g_u8Diag_Databuff_Right[1] == TPS_CHIP_ADD_15)
			{
				s_st_Trunk_DiagStatus.Board_Loss_Fault_Right = 0;
			}
		}
	}
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
	if(position == 1)
	{
		if(g_u8Diag_Databuff[5] == Tps_CrcCalc(&g_u8Diag_Databuff[4], 1))
		{
			if((g_u8Diag_Databuff[4] & 0x10) == 0x10)
			{
				if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_2)
				{
					s_st_Fender_DiagStatus.Open_Short_Fault = 1;
					
				}
			}
			else
			{
				if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_2)
				{
					s_st_Fender_DiagStatus.Open_Short_Fault = 0;
				}
			}
		}
		
		if(g_u8Diag_Databuff[7] == 0xFF)
		{
			if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_2)
			{
				s_st_Fender_DiagStatus.Board_Loss_Fault = 1;
				g_u8Diag_Databuff[7] = 0x00;
			}
		}
		else
		{
			if(g_u8Diag_Databuff[1] == TPS_CHIP_ADD_2)
			{
				s_st_Fender_DiagStatus.Board_Loss_Fault = 0;
				
			}
		}
	}
#endif
}



